﻿using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Com.Seezt.Skins;
using Components;
using Controls;
using UI.Lib;
using UI.Lib.Factory;

namespace UI.Invoice
{
    public partial class FrmTic1a10Medit : UI.ParentForm.BaseMedit
    {
        public FrmTic1a10Medit(string modulecode, string action, string text, string key)
            : base(modulecode, action)
        {
            InitializeComponent();

            this.Text = text;
            this.Key = key;

            //回写的方法
            this.SaveCallFuntion = "Tic1a10Manager$SaveData"; //保存的方法
            IsAfterSaveCloseWin = false;
            this.MaxStateFlag = true;
            this.allowDtlEmpty = false; //可能用户一次想把全部的记录删除
            if (!string.IsNullOrEmpty(this.Key))
            {
                IsAfterSaveCloseWin = true;
            }
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            if ("sellercode" == name)
            {
                where = string.Format(" status ='1' {0} ", Context.GetAppKeyWhere("usercode", true));
            }

            return true;
        }

        protected override bool BeforeDtlPopUp(string name, ref string where, ref string[] others)
        {
            if ("shopcode" == name)
            {
                var sellerid = Validation.ToString(this.tbSummary.GetValue("cesellerid"));
                if (string.IsNullOrEmpty(sellerid))
                {
                    MessageBox.Show("请先选择销售商！", "提示");
                    this.tbSummary.Focus("sellercode");
                    return false;
                }

                where = string.Format("slistid = '{0}' {1} ", sellerid, Context.GetAppKeyWhere("muser", true));
            }
            else if ("subcode" == name)
            {
                where = string.Format("substatue = '1' {0} ", Context.GetAppKeyWhere("usercode", true));
            }

            return true;
        }

        protected override void AfterSaveData(bool isSuccess)
        {
            if(isSuccess)
            {
                this.tbSummary.Enable("sellercode", true);
            }
        }

        protected override void PageInit()
        {
        }

        private bool isReadOnly = false;
        protected override void AfterAddRow()
        {
            if (isReadOnly == true) return;

            this.tbSummary.Enable("sellercode", false);
        }

        protected override void AfterDelete()
        {
            if (this.dgvDtl.Rows.Count == 0)
            {
                this.tbSummary.Enable("sellercode", true);
                isReadOnly = false;
            }
        }

        protected override void AfterDtlValueChanged(string name, object value)
        {
            if ("cedono" == name)
            {
                if (Validation.IsNull(value)) return;

                System.Collections.Hashtable hs = new System.Collections.Hashtable();
                hs["appid"] = UI.Lib.Context.realComcode;
                hs["docno"] = value;
                string reflt = UI.Lib.Factory.CommonHelper.Entity.ExecFunScalar("checkTic1a10docno", hs);
                if (!string.IsNullOrEmpty(reflt))
                {
                    MessageBox.Show("单号(" + value + ")已经存在于数据库中，请重新录入！", "提示");
                    this.tbSummary.ClearItem(name);
                }
            }
        }
    }
}
